package com.zjs.practice.service.impl;

import com.zjs.practice.mapper.MysqlPracticeMapper;
import com.zjs.practice.service.MysqlPracticeService;
import com.zjs.practice.domain.AffairTest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 数据库事务练习
 * @author zhu
 * @version 1.0
 * @date 2021/1/30 14:35
 */
@Slf4j
@Service
@Transactional
public class MysqlPracticeServiceImpl implements MysqlPracticeService {


    @Resource
    private MysqlPracticeMapper mapper;

    @Override
    public void test() throws Exception {
        AffairTest affairTest = mapper.query(new AffairTest());
        if (affairTest.getStock().equals(100)) {
            Integer num = new Double(Math.random() * 100).intValue();
            log.info("修改人：{}", num);
            affairTest.setStock(num);
            affairTest.setUpdatePerson(num.toString());
            mapper.update(affairTest);
            AffairTest query = mapper.query(new AffairTest());
            log.info("修改后的数据：{}", query);
            if (!query.getStock().equals(num)) {
                throw new Exception("数据异常");
            }
        }
    }

}
